package com.srwl.mytx.widget;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.srwl.mytx.R;

import java.util.Timer;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LiveCountDownView extends LinearLayout {

    private static final String TAG = "CountDownView";
    @BindView(R.id.pb_count_down)
    ProgressBar pb_countDown;
    @BindView(R.id.tv_progress)
    TextView tv_progress;
    @BindView(R.id.tv_max_duration)
    TextView tv_maxDuration;
    //    @BindView(R.id.sb_count_down)
//    SeekBar sbCountDown;
//    @BindView(R.id.mySeekBar)
//    VerticalSeekBar verticalSeekBar;

    private int totalDuration;
    private int progress;//当前时长进度
    private boolean isStarted;
    private Timer timer;
    private InteractionListener listener;
    private Context context;
    private Handler handler = new Handler();
    private Runnable countDownRunnable = new Runnable() {
        @Override
        public void run() {
            if (progress >= 0) {
                pb_countDown.setProgress(progress);
                //sbCountDown.setProgress(progress);
                //verticalSeekBar.setProgress(progress);
                tv_progress.setText(progress + "");
                handler.postDelayed(countDownRunnable, 1000);
                progress--;

            } else {
                Log.d(TAG, "run: 直播时限已到");

                setVisibility(View.INVISIBLE);
                isStarted = false;
                if (listener != null) {
                    listener.onTimeOut();
                }

            }
        }
    };

    public LiveCountDownView(Context context) {
        super(context);
        this.context = context;
        inflate(context, R.layout.live_room_count_down, this);
        ButterKnife.bind(this);
    }

    public LiveCountDownView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        inflate(context, R.layout.live_room_count_down, this);
        ButterKnife.bind(this);
    }


    public void startCount(int totalDuration, int progress) {
        isStarted = true;
        this.totalDuration = totalDuration;
        this.progress = progress;
        setVisibility(VISIBLE);
        //tv_progress.setText(totalDuration + "");
        pb_countDown.setMax(totalDuration);
        //verticalSeekBar.setMax(totalDuration);
        //sbCountDown.setMax(totalDuration);
        tv_maxDuration.setText(totalDuration + "");
        // countDown();
        handler.post(countDownRunnable);
    }

    public boolean isStarted() {
        return isStarted;
    }

    public void setListener(InteractionListener listener) {
        this.listener = listener;
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }


    public void stopCount() {
        isStarted = false;
        handler.removeCallbacks(countDownRunnable);
        setVisibility(INVISIBLE);
    }

    public void clear(){
        handler.removeCallbacks(countDownRunnable);

    }


    public interface InteractionListener {
        void onTimeOut();
    }

}
